package class202205.demo10.demo08;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card{
    private String suit;
    private int rank;

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "[ " +suit +" "+rank+" ]";
                /*"Card{" +
                "suit='" + suit + '\'' +
                ", rank=" + rank +
                '}';*/
    }
}


public class TestCard {

    public static final String[] suits={"♥","♠","♣","♦"};

    public static List<Card> buyCard(){
        List<Card> desk = new ArrayList<>();//放入实例化后的数据
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                String suit = suits[i];//花色是i下标
                Card card = new Card(suit,j);//数值是j     实例好后放到第一个for上面
                desk.add(card);//与上面实例化对应  全都放进来
            }
        }
        return desk;
    }
    public static void shuffle(List<Card> cardList){//洗牌先传牌（传的牌）
        for (int i = cardList.size()-1; i > 0 ; i--) {
            Random random = new Random();
            int index = random.nextInt(i);//生成的下标，给定范围i
            //下面交换方法写好之后，在这里定义一下
            swap(cardList,i,index);
        }
    }
    //洗牌的交换环节
    private static void swap(List<Card> cardList,int i,int j){
        /**
         * int tmp = array[i];
         * array[i] = array[j];
         * array[j] = tmp;
         */
        //选定i下标和j下标交换  （牌之间的交换）
        //i下标或者j下标的牌需要get来获取 因为是私有的
        Card tmp = cardList.get(i);
        //跟上面注释的交换类似()里面左右交换
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

    public static void main(String[] args) {
        List<Card> cardList = buyCard();//buyCard()的返回值类型  List<Card>
        System.out.println("买牌:"+cardList);

        shuffle(cardList);
        System.out.println("洗牌:"+cardList);

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hands = new ArrayList<>();  //给一个二维数组

        //放入数组中
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        //5个人轮流揭牌五张  i=0第一次
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //因为洗牌后全都存到card  那么每次揭牌都去获取cardList   0下标的数据--->相当于删除
                Card card = cardList.remove(0);
                //hands.get(j).add(i,card);
                //分解上面的代码
                List<Card> hand = hands.get(j);
                hand .add(i,card);


            }
        }

        System.out.println("第一个人的牌:"+hand1);
        System.out.println("第二个人的牌:"+hand2);
        System.out.println("第三个人的牌:"+hand3);

        System.out.println("剩余的牌:"+cardList);
    }
}
